<template>
	<view class="update-password-layout">
		<view class="update-password-form">
			<xh-input :showTitle="true" title="原密码" minWidth="180rpx" placeholder="请输入原密码" padding="30rpx" v-model="formParams.oldPassword" :isEye="true" :password="true" :maxCount="18"></xh-input>
			<xh-input :showTitle="true" title="新密码" minWidth="180rpx" placeholder="6~18位,数字+字母组合" padding="30rpx" v-model="formParams.password" :isEye="true" :password="true" :maxCount="18"></xh-input>
			<xh-input :showTitle="true" title="确认新密码" minWidth="180rpx" placeholder="再次输入新密码" padding="30rpx" v-model="formParams.surePassword" :isEye="true" :password="true" :maxCount="18"></xh-input>
		</view>
		<view class="submit-btn" :style="{marginTop:'50rpx'}" @tap="submit">提交</view>
	</view>
</template>

<script setup>
	import { ref } from 'vue'
	import { updatePassword } from '@/common/api/api.js'
	import { useAccountStore } from '@/common/stores'
	const accountStore = useAccountStore()
	const formParams = ref({
		oldPassword:'',
		password:'',
		surePassword:''
	})
	// 提交
	const submit = async ()=>{
		if(uni.$uv.test.empty(formParams.value.oldPassword)) {
			uni.$utils.showToast('请输入原密码')
			return
		}
		else if(uni.$uv.test.empty(formParams.value.password)) {
			uni.$utils.showToast('请输入新密码')
			return
		}
		else if(!uni.$utils.checkPassword(formParams.value.password)) {
			uni.$utils.showToast('密码必须是6~18位数字+字母组合')
			return
		}
		else if(uni.$uv.test.empty(formParams.value.surePassword)) {
			uni.$utils.showToast('请再次输入新密码')
			return
		}
		else if(formParams.value.password != formParams.value.surePassword) {
			uni.$utils.showToast('两次输入的密码不一致')
			return
		}
		let params = {}
		const oldPwdRes = uni.$utils.passwordEncrypt(formParams.value.oldPassword)
		const pwdRes = uni.$utils.passwordEncrypt(formParams.value.password)
		params.password = oldPwdRes
		params.newPassword = pwdRes
		params.confirmPassword = pwdRes
		await updatePassword(params)
		if(!uni.$uv.test.empty(accountStore.password)){
			accountStore.password = formParams.value.password
		}
		uni.redirectTo({
			url:'/pages/login/reset-result/reset-result?title=修改密码'
		})
	}
</script>

<style lang="scss" scoped>
	.update-password-layout {
		padding-top: 1px;
		.update-password-form {
			background-color: #ffffff;
		}
	}
</style>